package com.xiaoma.lock.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestPC {

  private volatile boolean printA = true;
  private Lock lock = new ReentrantLock();
  private Condition condition = lock.newCondition();

  public void printA() {
    try {
      lock.lock();
      while (printA) {
        condition.await();
      }
      System.out.println("AAAAA");
      printA = true;
      condition.signal();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void printB() {
    try {
      lock.lock();
      while (!printA) {
        condition.await();
      }
      System.out.println("BBBBB");
      printA = false;
      condition.signal();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }
}
